
//
//  BAFooterView.m
//  BabyAlbum
//
//  Created by master on 17/3/1.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAFooterView.h"

@interface BAFooterView () <UITextViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UITextField *titleNode;
@property (nonatomic, strong) IBOutlet UIButton *dateButton;
@property (nonatomic, strong) IBOutlet UILabel *weekNode;
//@property (nonatomic, strong) UIView *lineNode;
@property (nonatomic, strong) IBOutlet UITextView *detailNode;
@property (nonatomic, strong) BAAlbumModel *model;
//@property (nonatomic, assign) BOOL isEditing;
@property (nonatomic, copy) void (^selectBlock)(void) ;
@property (nonatomic, copy) void (^titleChangeBlock)(NSString *text) ;
@property (nonatomic, copy) void (^detailTextChangeblock)(NSString *text) ;

@property (nonatomic, copy) void (^datePickerBlock)(void) ;

@end

@implementation BAFooterView

- (void)setData:(BAAlbumModel *)model titleChangeBlock:(void (^)(NSString *text))titleChangeBlock detailChangeBlock:(void (^)(NSString *text))detailTextChangeblock datePickerBlock:(void (^)(void))datePickerBlock {
    self.titleChangeBlock = titleChangeBlock;
    self.detailTextChangeblock = detailTextChangeblock;
    self.datePickerBlock = datePickerBlock;
    self.model = model;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:_titleNode];
    _titleNode.placeholder = @"点击输入标题";

    _titleNode.text = model.title;
    
    _titleNode.delegate = self;
    
    
    [_dateButton setTitle:[[model customDateShow] length]?[model customDateShow]:[[NSDate date] showDate] forState:UIControlStateNormal];
    
    _weekNode.text = [[model weekDayShow] length]?[model weekDayShow]:[[NSDate date] weekDayShow] ;
    
    
    [_detailNode setPlaceholderWithText:@"用文字记录难忘瞬间" Color:_detailNode.textColor];//.placeholder = @"用文字记录难忘瞬间";
    _detailNode.text = model.desc;
    _detailNode.delegate = self;
    
    [self setUpForDismissKeyboard];
}

- (void)setDate:(NSDate *)date {
    [_dateButton setTitle:[date showDate] forState:UIControlStateNormal];
    _weekNode.text = [date weekDayShow];
}



- (void)textViewDidChange:(UITextView *)textView {
    if(textView==_detailNode && self.detailTextChangeblock) {
        self.detailTextChangeblock(textView.text);
    }
}


- (void)textFieldTextDidChange:(NSNotification *)notification{
    UITextField *textField=[notification object];
    if(textField==_titleNode && self.titleChangeBlock) {
        self.titleChangeBlock(textField.text);
    }
}



- (IBAction)dateSelect {
    if(self.datePickerBlock) {
        self.datePickerBlock();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

//TODO: 高度计算错误
+ (CGFloat)calculteHeightWithData:(BAAlbumModel *)albumModel {
//    CGFloat descHeight = [albumModel.desc uxy_calculateSize:CGSizeMake(kScreenWidth - 30, CGFLOAT_MAX) font:[UIFont systemFontOfSize:11]].height;
    
    CGFloat descHeight = [UILabel getHeightByWidth:kScreenWidth - 30 title:albumModel.desc font:[UIFont systemFontOfSize:13]];
    if(descHeight < 200 ) {
        descHeight = 200;
    }
    CGFloat height = 5 + 20 + 5 + 13.5 + 5 + 1+ 5 + descHeight + 20;
    return height;
}


#pragma show and hide the keyboard
- (void)setUpForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [KEYWINDOW addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [KEYWINDOW removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
//    此method会将self.view里所有的subview的first responder都resign掉
    
//    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
//    UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
//    
//    NSLog(@"%@",firstResponder);
//    [self.detailNode resignFirstResponder];
//    firstResponder = [keyWindow performSelector:@selector(firstResponder)];
//    
//    NSLog(@"%@",firstResponder);
//
//    [self.titleNode resignFirstResponder];
//    firstResponder = [keyWindow performSelector:@selector(firstResponder)];
//    
//    NSLog(@"%@",firstResponder);

    
    
    [self.view endEditing:YES];
//    firstResponder = [keyWindow performSelector:@selector(firstResponder)];
//    NSLog(@"%@",firstResponder);

}

@end
